layui.define(['form','table','tree', 'layer','fault_flow','http_proxy'],function(exports){
    var $ = layui.$,
    http_proxy = layui.http_proxy

    var data = {
        //页面加载完毕后，调取后端接口。将抢修队画在地图上
        drawTeamsMarker : function (){
            var that=this;
            $.ajax({
                url:http_proxy('/fault/getEmergencyTeams'),
                type:'get',
                async: true,
                responseType: 'json',
                success: function (rep) {
                    let emergencyTeamsList = new Array();
                    rep.data.emergencyTeams.forEach(function(val,index,arr){
                        emergencyTeamsList.push({
                            markType: 3,
                            id: val.queueId,
                            name: val.queueName,
                            x: val.xaxis,
                            y: val.yaxis,
                            etcData: {
                                name: val.queueName,
                                content: val.queueContent,
                                user: val.queueUser,
                                major: val.remark3,
                                phone: val.queuePhone
                            }
                        })
                    })
                    maputil.__draw(2,emergencyTeamsList,that.clickEmergencyTeam);
                }
            })
        },

        //今日未完成应急事件
        drawTodayFault: function(rep){
            let erroList = new Array();
            rep.data.forEach(function(val,index,arr){
                erroList.push({
                    markType: 1,
                    faultMarkType: 'new',
                    dataType: val.dataType,
                    id: val.faultId,
                    name: val.faultContent,
                    x: val.xaxis,
                    y: val.yaxis
                })
            })
            maputil.closeInfoWindow();
            maputil.removeHistory();
            maputil.__draw(1,erroList,this.clickTodayFaultMarker);
        },

        //将正线值守点画在地图上
        drawDutyPoint: function(rep){
            let that = this;
            let dutyList = new Array();
            rep.data.forEach(function(val,index,arr){
                if(val.xaxis && val.yaxis){
                    dutyList.push({
                        markType: 4,
                        id: val.dutyId,
                        name: val.dutyName,
                        x: val.xaxis,
                        y: val.yaxis,
                        etcData: {
                            name: val.dutyName,
                            company: val.dutyCompany,
                            dutyPro: val.dutyPro,
                            className: val.className,
                            phone: val.phone,
                            dutyBigPro: val.dutyBigPro
                        }
                    })
                }
            })
            maputil.__draw(1,dutyList,that.clickDutyPointMarker);
        },

        clickTrueTimeFault: function (data) {
            var that=this
            $.ajax({
                url: http_proxy('/fault/getHanderFaultUserInfo'),
                type:'post',
                async: 'true',
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify({faultId: data.faultId}),
                success: function (rep) {
                    if(rep.data.length > 0){
                        let personList = new Array();
                        rep.data.forEach(function(val,index,arr){
                            personList.push({
                                markType: 2,
                                faultType: val.faultType,
                                dataType: val.dataType,
                                id: val.id,
                                name: val.userName,
                                x: val.xaxis,
                                y: val.yaxis,
                                etcData: {
                                    name: val.userName,
                                    className: val.className,
                                    phone: val.phone
                                }
                            })
                        });
                        maputil.__draw(1,personList,that.clickPersonMarker);
                    }else{
                        maputil.removeHuman();
                    }
                    
                    maputil.removeHistory();
                    maputil.showCircles();
                }
            });
            maputil.closeInfoWindow();
            maputil.circlesArray.forEach(function(val,index,arr){
                if(val.id == data.faultId){
                val.emit('click');
                }
            })
            //2、将地图聚焦到响应的应急事件上
            map.setZoomAndCenter(11,[data.xaxis || 121.414776,data.yaxis || 31.184571]);
        },

        // 调取后台接口，将今日已完成或历史事件画到地图上，打开标签
        //isHistory是否为历史
        drawHistoryOrCompleteFault: function (obj,isHistory){
            var that=this;
            let reqData;
            if(isHistory){
                reqData = {
                    'faultId': obj.data.faultId+'',
                    'type': 2+''
                }
            }else {
                reqData = {
                    'faultId': obj.data.faultId+'',
                    'type': 1+'',
                    'dataType': obj.data.dataType+''
                }
            }
            $.ajax({
                url: http_proxy('/fault/getFaultInfoById'),
                type: 'post',
                async: 'true',
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify(reqData),
                success: function (rep) {
                    let data = rep.data.faultInfo;
                    maputil.removeHistory();
                    maputil.removeHuman();
                    //将应急事件画到地图上
                    let etcData = isHistory ? 
                    {
                        faultTime: data.faultTime,
                        zy: data.zy,
                        faultLine: data.faultLine,
                        bx: data.bx,
                        faultLocation: data.faultLocation,
                        gzSb: data.gzSb,
                        faultContent: data.faultContent
                    } : 
                    {
                        disposePeopleNum: data.disposePeopleNum,
                        faultTime: data.faultTime,
                        userName: data.userName,
                        faultLocation: data.faultLocation,
                        leaderPhone: data.leaderPhone,
                        faultContent: data.faultContent
                    }
                    let markerList = maputil.__draw(3,
                        [{
                            markType:1,
                            faultMarkType: 'old',
                            id: data.faultId,
                            x:data.xaxis,
                            y:data.yaxis,
                            statusButton: data.gzFlag,
                            etcData: etcData
                        }]
                    ,isHistory ? that.clickHistoryMarker : that.clickCompleteMarker);
                    markerList[0].emit('click');

                    map.setZoomAndCenter(11,[data.xaxis || 121.414776,data.yaxis || 31.184571]);
                }
            })
        },
        
        //点击今日应急事件
        clickTodayFaultMarker: function (faultId,dataType){
            let reqData = {
                'faultId': faultId+'',
                'type': 1+'',
                'dataType': dataType+''
            }
            var windowInfo = {};
            var showParam = {};
            $.ajax({
                url: http_proxy('/fault/getFaultInfoById'),
                type: 'post',
                async: false,
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify(reqData),
                success: function(rep){
                    let data = rep.data.faultInfo;
                    showParam = {
                        disposePeopleNum: data.disposePeopleNum,
                        faultTime: data.faultTime,
                        userName: data.userName,
                        faultLocation: data.faultLocation,
                        leaderPhone: data.leaderPhone,
                        faultContent: data.faultContent
                    };
                    windowInfo.showParam = showParam;
                    windowInfo.windowType = 'fault';
                }
            })
            return windowInfo;
        },

        //点击应急抢修队
        clickEmergencyTeam: function(id,dataType,etcData){
            var windowInfo = {};
            windowInfo.showParam = JSON.parse(JSON.stringify(etcData));;
            windowInfo.windowType = 'teams';
            return windowInfo;
        },
        
        //点击人员图标
        clickPersonMarker: function (id,dataType,etcData){
            var windowInfo = {};
            windowInfo.showParam = JSON.parse(JSON.stringify(etcData));;
            windowInfo.windowType = 'person';
            return windowInfo;
        },
        
        //点击历史应急事件图标
        clickHistoryMarker: function (id,dataType,etcData,buttonType){
            var windowInfo = {};
            windowInfo.showParam = JSON.parse(JSON.stringify(etcData));
            windowInfo.windowType = 'history';
            windowInfo.buttonType = buttonType;
            return windowInfo;
        },

        //点击今日已完成应急事件图标
        clickCompleteMarker: function (id,dataType,etcData,buttonType){
            var windowInfo = {};
            windowInfo.showParam = JSON.parse(JSON.stringify(etcData));
            windowInfo.windowType = 'complete';
            windowInfo.buttonType = buttonType;
            return windowInfo;
        },

        clickDutyPointMarker: function (id,dataType,etcData,buttonType){
            var windowInfo = {};
            windowInfo.showParam = JSON.parse(JSON.stringify(etcData));
            windowInfo.windowType = 'duty';
            windowInfo.buttonType = buttonType;
            return windowInfo;
        },
        
        //页面加载完毕后，加载前端写死的装备点
        loadEmergency: function(){
            maputil.loadEmergency()
        },
        
        maputilInit: function (){
            maputil.__init()
        },

        fallbackToTrueTime: function () {
            maputil.showCircles();
            maputil.removeHistory();
            maputil.closeInfoWindow();
        },

        queryHistory: function () {
            maputil.closeInfoWindow();
            maputil.removeHuman();
            maputil.removeHistory();
            maputil.hideCircles();
        },

        clearDutyPointMarker: function() {
            maputil.removeDuty();
            maputil.closeInfoWindow();
        }
    }
    
    exports('mapDrawClick', data)
});